<?php
class SkinsController extends Zend_Controller_Action
{
    public function init(){;
        $formLogin = new Application_Form_Login();
        $this->view->formLogin = $formLogin;
        $model =new Application_Model_Noticias();
        $this-> view->noticias=$model->getAll();
        $modelTesis =new Application_Model_Tesis();
        $this-> view->tesis=$modelTesis->getAll();
        $modelPersona =new Application_Model_Personas();
        $this-> view->persona=$modelPersona->getAll();
        if(Zend_Auth::getInstance()->hasIdentity()){
            $skin=Zend_Auth::getInstance()->getIdentity()->skin;
            $this->_helper->Cambiarskin($skin);
        } 
    }
    public function skinAction() {
     
        $formSkin=new Application_Form_Skin();    
        if($this->getRequest()->isPost()){
            if($formSkin->isValid($this->_getAllParams())){
                $modelSkin=new Application_Model_Usuarios();
                $modelSkin->cambiarskin(Zend_Auth::getInstance()->getIdentity()->id_usuario,$formSkin->getValue('skin'));
                
                if($formSkin->getValue('skin')==0){
                    $options = array('layout' => 'layout',);  
                    Zend_Layout::startMvc($options);
                }   
                if($formSkin->getValue('skin')==1){
                    $options = array('layout' => 'nskin',);  
                    Zend_Layout::startMvc($options);
                }
                if($formSkin->getValue('skin')==2){
                    $options = array('layout' => 'nskin2',);  
                    Zend_Layout::startMvc($options);
                }
                if($formSkin->getValue('skin')==3){
                    $options = array('layout' => 'cskin',);  
                    Zend_Layout::startMvc($options);
                }
                if($formSkin->getValue('skin')==4){
                    $options = array('layout' => 'layout2',);  
                    Zend_Layout::startMvc($options);
                }
             
                $authAdapter=$this->getAuthAdapter();
                $username=Zend_Auth::getInstance()->getIdentity()->usuario;
                $password=Zend_Auth::getInstance()->getIdentity()->password;
  
                Zend_Auth::getInstance()->clearIdentity();
          
                $authAdapter->setIdentity($username)
                            ->setCredential($password);
                $auth = Zend_Auth::getInstance();
                $result = $auth->authenticate($authAdapter);
                if( $result->isValid() ){
                    $identity=$authAdapter->getResultRowObject();
                    $authStorage=$auth->getStorage();
                    $authStorage->write($identity);
                }
            }
        }
        $this->view->formSkin=$formSkin;
    }
    private function getAuthAdapter(){
          $authAdapter=new Zend_Auth_Adapter_DbTable(Zend_Db_Table::getDefaultAdapter());
          $authAdapter->setTableName('usuario')
                            ->setIdentityColumn('usuario')
                            ->setCredentialColumn('password');
          return $authAdapter;
    }
}